package _2FindAlgorithm.src.BinarySearch;

public class Demo1_BinarySearch {
    public static void main(String[] args) {
        int[] st = new int[]{1, 3, 4, 6, 7, 9, 23, 45, 56};
        int s = binarySearchBasic(st, 9);
        if (s != -1) {
            System.out.println("找到该值了,在索引" + s + "处");
        } else {
            System.out.println("未找到该值");
        }
    }

    public static int binarySearchBasic(int[] a, int target) {
        int min = 0;
        int max = a.length - 1;

        while(min <= max) {//注意结束条件
            int middle = min + max >>> 1;//大数据存储
            if (target < a[middle]) {
                max = middle - 1;//改变位置
            }

            if (a[middle] < target) {
                min = middle + 1;//改变位置
            }

            if (target == a[middle]) {
                return middle;//找到直接返回
            }
        }

        return -1;
    }
}
